package es.mgil.linternas;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TableLayout;
import android.widget.TableRow;

public class LinternaPolicia extends Activity {
	private int blueColor;
	private int redColor;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		blueColor = getResources().getColor(R.color.blueColor);
		redColor = getResources().getColor(R.color.redColor);

		setContentView(new LinternaPoliciaTableLayout(this));
	}

	private class LinternaPoliciaTableLayout extends TableLayout {
		private TableRow arriba;
		private TableRow abajo;

		public LinternaPoliciaTableLayout(Context context) {
			super(context);

			LayoutParams lp = new LayoutParams();
			lp.width = LayoutParams.MATCH_PARENT;
			lp.height = LayoutParams.MATCH_PARENT;
			setLayoutParams(lp);

			arriba = new PoliTableRow(context, redColor);
			lp = new LayoutParams();
			lp.weight = 0.5f;
			arriba.setLayoutParams(lp);
			addView(arriba);

			abajo = new PoliTableRow(context, blueColor);
			lp = new LayoutParams();
			lp.weight = 0.5f;
			abajo.setLayoutParams(lp);
			addView(abajo);

			this.setBackgroundColor(0xffffffff);
		}

		@SuppressWarnings("deprecation")
		@Override
		protected void onDraw(Canvas canvas) {
			Drawable arribaDrawable = arriba.getBackground();
			Drawable abajoDrawable = abajo.getBackground();

			arriba.setBackgroundDrawable(abajoDrawable);
			abajo.setBackgroundDrawable(arribaDrawable);

			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
			} finally {
				invalidate();
			}
		}
	}

	private class PoliTableRow extends TableRow {
		public PoliTableRow(Context context, int colorResource) {
			super(context);
			setBackgroundColor(colorResource);
		}
	}
}
